package com.ficus.road.maintain.controller;

import com.ficus.road.maintain.core.model.Car;
import com.ficus.road.maintain.core.model.InspectPictureVO;
import com.ficus.road.maintain.core.model.InspectionRequestDTO;
import com.ficus.road.maintain.service.CarVideoRecordService;
import com.ficus.road.maintain.service.InspectService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @author xiawen yang
 * @date 2021/10/11 上午10:45
 */
@RestController
@RequestMapping("/lesion")
public class InspectController {

    @Autowired
    private InspectService inspectService;

    @Autowired
    private CarVideoRecordService carVideoRecordService;

    /**
     * 查询出车信息(下拉框)
     *
     * @param date
     * @return
     */
    @GetMapping("/car/{date}")
    public ResponseEntity<List<Car>> getLesionInfo(@PathVariable("date") String date) {
        List<Car> cars = carVideoRecordService.findCars(date);
        return new ResponseEntity<>(cars, HttpStatus.OK);
    }

    /**
     * 查询定时上报截图
     *
     * @param inspectionRequestDTO
     * @return
     */
    @PostMapping("/labeling/query")
    public ResponseEntity<List<InspectPictureVO>> getLesionInfo(@RequestBody InspectionRequestDTO inspectionRequestDTO) {
        Query query = new Query();
        if (ObjectUtils.isNotEmpty(inspectionRequestDTO.getCarId())) {
            query.addCriteria(Criteria.where("car_id").is(inspectionRequestDTO.getCarId()));
        }
        if (ObjectUtils.isNotEmpty(inspectionRequestDTO.getStatus())) {
            if (inspectionRequestDTO.getStatus().equals(0)) {
                query.addCriteria(Criteria.where("flag").is(0));
            } else {
                query.addCriteria(Criteria.where("flag").in(1, 2));
            }
        }
        if (ObjectUtils.isNotEmpty(inspectionRequestDTO.getTime())) {
            //传过来的是日期零时的时间戳(秒级),一天86400s
            long start = inspectionRequestDTO.getTime();
            long end = start + 86400;
            query.addCriteria(Criteria.where("time_stamp").gte(start * 1000).lte(end * 1000));
        }
        // 经纬度不是0
        query.addCriteria(Criteria.where("gps_location.longitude").ne(0).and("gps_location.latitude").ne(0));
        List<InspectPictureVO> inspectPictureVOS = inspectService.queryLesionInfo(query);
        return new ResponseEntity<>(inspectPictureVOS, HttpStatus.OK);
    }

    @PostMapping("/labeling")
    public ResponseEntity<String> updateLesionInfo(@Validated @RequestBody InspectPictureVO inspectPictureVO) throws IOException {
        String pictureId = inspectService.updateLesionInfo(inspectPictureVO);
        return new ResponseEntity<>(pictureId, HttpStatus.OK);
    }
}
